package com.sye.reflectmodule;

import com.sye.reflectmodule.javabean.Person;
import org.junit.Test;
import org.reflections.Reflections;
import org.reflections.scanners.*;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * reflections 反射框架的使用
 *  主要作用是批量类的反射
 *  https://github.com/ronmamo/reflections
 */
public class ReflectionsFrameworkOpr {
    /**
     * 获取响应的类信息
     */
    @Test
    public void demoInit(){
        // 实例化Reflections，并指定要扫描的包名
        Reflections reflections = new Reflections("com.sye.reflectmodule.javabean");
        /**
         * 获取某一个类的所有子类
         */
        Set<Class<? extends Person>> subTypesOf = reflections.getSubTypesOf(Person.class);
        System.out.println(subTypesOf);//com.sye.reflectmodule.javabean.Student
        /**
         * 获取包含某个注解的所有类
         */
        Set<Class<?>> annotatedWith = reflections.getTypesAnnotatedWith(Deprecated.class);
        System.out.println(annotatedWith);//com.sye.reflectmodule.javabean.Student
    }
    /**
     * 框架的操作使用
     * 每一步的操作都要有对应的Scanner
     */
    @Test
    public void execOption(){
        // 实例化Reflections，并指定要扫描的包名
//        Reflections reflections = new Reflections("com.sye.reflectmodule.javabean");
        Reflections reflections = new Reflections(new ConfigurationBuilder()
                .setUrls(ClasspathHelper.forPackage("com.sye.reflectmodule.javabean"))
                .setScanners(new SubTypesScanner(),
                        new TypeAnnotationsScanner(),
                        new MethodAnnotationsScanner(),
                        new FieldAnnotationsScanner(),
                        new MethodParameterScanner())); //不加这些scanner 代码会报错的
        /**
         * 扫描方法包含Deprecated注解的类
         * 普通方法和构造方法
         */
        Set<Method> resources =
                reflections.getMethodsAnnotatedWith(Deprecated.class);
        Set<Constructor> injectables =
                reflections.getConstructorsAnnotatedWith(Deprecated.class);
        /**
         * 扫描字段注解
         */
        Set<Field> fieldsAnnotatedWith = reflections.getFieldsAnnotatedWith(Deprecated.class);
        System.out.println(fieldsAnnotatedWith);
        /**
         * 扫描方法参数(MethodParameterScanner)
         */
        Set<Method> someMethods =
                reflections.getMethodsMatchParams(long.class, int.class);
        Set<Method> voidMethods =
                reflections.getMethodsReturn(void.class);
        /*Set<Method> pathParamMethods =
                reflections.getMethodsWithAnyParamAnnotated(PathParam.class);*/

        //MethodParameterNamesScanner 扫描方法参数名称
        /*List<String> parameterNames =
                reflections.getMethodParamNames(Method.class);*/

        // 扫描方法调用情况
        //MemberUsageScanner
        /*Set<Member> usages =
                reflections.getMethodUsages(Method.class)*/
    }
}
